//
//  AppDelegate.swift
//  AIGirl
//
//  Created by Rae on 2023/12/22.
//

import UIKit

@_exported import Stevia
@_exported import IQKeyboardManager
import StoreKit
import SwiftyStoreKit

struct AGirlConstants {
    static let terms    = "https://www.baidu.com"
    static let policy    = "https://www.baidu.com"
    static let baseUrl      = "http://vivi.mimodev.cn:8080"
//    static let baseUrl      = "http://192.168.110.98:4399"
}

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UorInXNetworkProtocol {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch. vivi.mimodev.cn:8080
        // http://192.168.110.98:4399
        // http://vivi.mimodev.cn:8080
        RequestParameterConfig.shared.baseUrlString = AGirlConstants.baseUrl
        RequestParameterConfig.shared.packageName = "Honeyaigirl"
        IQKeyboardManager.shared().isEnabled = true
        InAppPurchaseNotify()
        initializeWindow()
        return true
    }

    private func initializeWindow(){
        window = UIWindow(frame: UIScreen.main.bounds)
//        let videoVC = ViewController()
//        videoVC.completion = {
//            
//        }
//        window?.rootViewController = videoVC
//        window?.makeKeyAndVisible()
        if let token = UserDefaultsManager.getString(forKey: .token) {
            RequestParameterConfig.shared.requestToken = token
            GlobalTool.refreshUserInfo()
            GlobalTool.hiddenLogin()
            let currentPage = UserDefaultsManager.getInt(forKey: .currentPage)
            if currentPage == 0 {
                self.window?.rootViewController = AIGirlTabBarViewController()
            }else if currentPage == 1{
                self.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlUserInfoNameViewController())
            }else if currentPage == 2{
                self.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlSetAgeViewController())
            }else if currentPage == 3{
                self.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlUserInfoChooseViewController())
            }else if currentPage == 4{
                self.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlUserInfoChooseViewController())
            }else if currentPage == 5{
                self.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlUserInfoTopicViewController())
            }
        }else{
            self.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlWelcomeViewController())
        }
        self.window?.makeKeyAndVisible()
        
    }
    
    func InAppPurchaseNotify() {
//        let receiptR = SKReceiptRefreshRequest()
//        receiptR.start()
//        SwiftyStoreKit.completeTransactions(atomically: true) { results in
//            for purchase in results {
//                if purchase.transaction.transactionState == .purchased || purchase.transaction.transactionState == .restored {
//                   if purchase.needsFinishTransaction {
////                       AIGirlApplePayCache.shared.productIdentifier = nil
////                       AIGirlApplePayCache.shared.sub_productIdentifier = nil
//                       // Deliver content from server, then:
//                       SwiftyStoreKit.finishTransaction(purchase.transaction)
//                   }
//                   print("purchased: \(purchase)")
//                }
//            }
//        }
    }
}

